import datetime
from sqlalchemy import Column, Integer, String, DateTime, func
from sqlalchemy.ext.declarative import declared_attr

from app.db.session import Base
from app.utils.timezone_util import now_china


class BaseModel(Base):
    """
    基础模型类
    """
    __abstract__ = True
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    create_by = Column(String(64), comment="创建人")
    create_time = Column(DateTime, default=now_china, comment="创建时间")
    update_by = Column(String(64), comment="修改人")
    update_time = Column(DateTime, onupdate=now_china, comment="修改时间")
    version = Column(Integer, default=1, comment="版本号")
    
    # 自动生成表名
    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()